fix(auth): propagate saveTokens errors during token refresh#2035
Open
jbsky wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(auth): propagate saveTokens errors during token refresh#2035jbsky wants to merge 1 commit intomodelcontextprotocol:mainfrom
jbsky wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Previously, auth() wrapped both refreshAuthorization() and saveTokens() in the same try/catch. Non-OAuthError exceptions (including I/O errors from saveTokens) were silently discarded. This is destructive when the AS uses rotating refresh tokens: the old RT is invalidated server-side upon issuing a new one, but since saveTokens failure was swallowed, the new tokens were lost. The client was left with an invalid refresh token and had to fully re-authenticate. Fix: move saveTokens() outside the try/catch so its errors propagate naturally. Only refreshAuthorization() failures are caught and allow fallthrough to a new authorization flow. Add test verifying saveTokens errors are propagated.
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2034
saveTokens()errors during token refresh are silently caught and discarded, becausesaveTokens()sits inside the sametry/catchthat handlesrefreshAuthorization()network errors. When an authorization server uses rotating refresh tokens, this is destructive: the AS has already invalidated the old RT, so losing the new tokens leaves the client permanently locked out.Changes
newTokensdeclaration outside thetryblocksaveTokens(newTokens)after thecatchblock so it is no longer swallowed by therefreshAuthorizationerror handlersaveTokenserrors propagate to the callerRoot cause
Test
Added test case: "should propagate saveTokens errors after successful token refresh" — verifies that a
saveTokensrejection is not caught by the refresh error handler.All 365 existing tests continue to pass.